<?php
/**
 * 监理通知单控制器
 * @author YangL
 **/
namespace Admin\Controller;
use Common\Controller\WahahabaseController;
class SupervisionNoticeController extends WahahabaseController{
    protected $deal_model;
    protected $department_model;

	public function _initialize() {
		parent::_initialize();
        $this->deal_model = D("Common/SupervisionNotice");
        $this->department_model=D("Common/Department");
		$this->check = array('code');
	}

	/**
     *  主页
     */
	public function index(){
		$where = array("d.is_deleted"=>array('neq', 1));
		$main_content = I('request.main_content');
		if($main_content){
			$where['main_content'] = array('like',"%$main_content%");
		}
        $this->getDepartLevel();
        $this->indexPage($where);
	}


    /**
     * 查询列表数据
     **/
    public function indexPage($where){
        $eq = array(
            "eq" => "=",
            "neq" => "!=",
            "gt" => ">",
            "egt" => ">=",
            "lt" => "<",
            "elt" => "<=",
            "like" => "LIKE"
        );
        // 当前登录人
        $adminId = session('ADMIN_ID');
        $adminGroup = getUserInfo($adminId);
        $condition = "(
            (d.is_publish = 0 AND (d.add_user = $adminId OR ((SELECT department_id FROM hzzy_users WHERE id=$adminId) = d.add_department) AND ((SELECT role_id FROM hzzy_users WHERE id=$adminId) IN('1','2','3'))))
            OR
            (d.is_publish = 1   
                AND (
                        (SELECT IFNULL(department_role,0) as `level` FROM hzzy_users u1 LEFT JOIN hzzy_department d1 ON u1.department_id = d1.id WHERE u1.id=$adminId)
                        <= 
                        (SELECT IFNULL(department_role,0) as `level` FROM hzzy_users u2 LEFT JOIN hzzy_department d2 ON u2.department_id = d2.id WHERE u2.id=d.add_user)
                )
                AND  '$adminGroup[department_role]' in ('1','2','3')
            )
            OR
            (d.is_publish = 1
                AND
                d.executing_department_management = '$adminGroup[department_id]' 
            )
            OR
            ((SELECT role_id FROM hzzy_users WHERE id=$adminId) = 0 AND d.is_publish = 1)
        )";
        foreach($where as $key => $v){
            if(count($v) > 1){
                $d = $eq[$v[0]] == NULL ? '=' : $eq[$v[0]];
                if($d != 'LIKE'){
                    $condition .= " AND $key $d '".$v[1]."'";
                } else {
                    $condition .= " AND $key $d '%".$v[1]."%'";
                }
            } else {
                $condition .= " AND $key = '$v'";
            }
        }

        $count=$this->deal_model->alias("d")->where($condition)->count();
        $page = $this->page($count, 20);

        $results = $this->deal_model->alias("d")
            ->field('d.*,u.real_name')
            ->join(array(
                ' LEFT JOIN '.C('DB_PREFIX').'users u ON d.add_user = u.id',
            ))
            ->where($condition)
            ->order("d.add_time DESC")
            ->limit($page->firstRow, $page->listRows)
            ->select();
        //
        foreach($results as $k =>  $v){
            if( is_numeric($v['executing_department_management']) ){
                //判断是数字则执行获取其单位名称
                $results[$k]['executing_department_management'] = $this -> department_model
                                                                            -> where(array('id'=>$v['executing_department_management']))
                                                                            -> getField('department_name');
                $results[$k]['executing_department_management'] .= "施工项目经理部";
            }
        }

        //
        $this->assign("page", $page->show('Admin'));
        $this->assign("results",$results);

        $this->display();
    }




    /**
     *  添加
     */
	public function add(){
        //筛选所有施工单位5级单位
        $Department = $this->department_model->where(array('is_deleted' => 0 , 'department_role'=>5))->field("id,department_name")->order("id DESC")->select();
        $this->assign("data", $Department);
		$this->display();
	}
	public function add_post(){
		if(IS_POST){
			$this->dataCheck();
			$data = array_merge($_POST, $this->saveData());
			if ($this->deal_model->create()!==false) {
				$result = $this->deal_model->add($data);
				if ($result!==false) {			
					$this->success("添加成功！", U("SupervisionNotice/index"));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->deal_model->getError());
			}
		}
	}
	
	/**
     *  修改
     */
	public function edit(){
        //筛选所有施工单位5级单位
        $Department = $this->department_model->where(array('is_deleted' => 0 , 'department_role'=>5))->field("id,department_name")->order("id DESC")->select();
        $this->assign("data", $Department);
		$id = I('get.id',0,'intval');
		$result = $this->deal_model->where(array("id"=>$id))->find();
		$this->assign("result", $result);
		
		$this->display();
	}
	public function edit_post(){
		if (IS_POST) {
			$this->dataCheck();
			$data = array_merge($_POST, $this->saveData(2));
			if ($this->deal_model->create()!==false) {
				$result=$this->deal_model->save($data);
				if ($result !== false) {
					$this->success("保存成功！", U("SupervisionNotice/index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->deal_model->getError());
			}
		}
	}
	public function  view(){
        //筛选所有施工单位5级单位
        $Department = $this->department_model->where(array('is_deleted' => 0 , 'department_role'=>5))->field("id,department_name")->order("id DESC")->select();
        $this->assign("data", $Department);
		$id = I('get.id',0,'intval');
		$data=$this->deal_model->where(array("id"=>$id))->find();
		$this->assign("result",$data);
		$this->getDepartLevel();
		$this->display();
	}
}


?>